<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="typeCode">
        <el-input v-model.trim="queryParams.typeCode" placeholder="请输入编码" clearable @keyup.enter.native="handleQuery" style="width: 130px" />
      </el-form-item>
      <el-form-item prop="typeName">
        <el-input v-model.trim="queryParams.typeName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" style="width: 130px" />
      </el-form-item>
      <el-form-item prop="specialTypeId">
        <el-select v-model="queryParams.specialTypeId" placeholder="专业类别" clearable filterable style="width: 130px">
          <el-option v-for="dict in specialTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditCheckType = true" v-hasPermi="['cq:checkType:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" size="small" height="calc(100vh - 150px)">
      <el-table-column label="阶段编码" prop="typeCode" min-width="70" />
      <el-table-column label="阶段名称" prop="typeName" show-overflow-tooltip min-width="200" />
      <el-table-column label="专业类别" align="center" min-width="70">
        <dict-tag slot-scope="{ row }" :options="specialTypes" :value="row.specialTypeId" />
      </el-table-column>
      <el-table-column label="是否启用" align="center" min-width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.isEnable ? 'primary' : 'info'">{{ row.isEnable ? '启用' : '禁用' }}</el-tag>
      </el-table-column>
      <el-table-column label="排序" prop="seq" align="center" width="50" />
      <el-table-column label="修改人" prop="updator" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="最后修改时间" align="center" show-overflow-tooltip min-width="120">
        <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditCheckType.open(row)" v-hasPermi="['cq:checkType:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-hasPermi="['cq:checkType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditCheckType ref="EditCheckType" v-model="showEditCheckType" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-阶段页面
import { getSpecialTypes } from '@/utils/dict/custom'
import { listCheckType, delCheckType } from '@/api/qualityManage/cq'
import EditCheckType from './components/EditCheckType'
export default {
  // eslint-disable-next-line
  name: '/cq/checkType',
  components: { EditCheckType },
  data() {
    return {
      loading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeCode: undefined,
        typeName: undefined,
        specialTypeId: undefined
      },
      showEditCheckType: false,
      specialTypes: []
    }
  },
  created() {
    getSpecialTypes().then(res => (this.specialTypes = res))
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      const res = await listCheckType(this.queryParams).catch(() => false)
      this.loading = false
      if (res === false) return

      const data = res.data || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm('确认删除阶段名称为"' + item.typeName + '"的数据项?', '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delCheckType(item.typeId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
